;
(function($,win)
{
	/* @------------------------------------------------------------------------------------@ */
	$(document).ready(function()
	{
		debugger;
		var url = location.search;
		if (url.indexOf("?merchantId=") != -1) {
			var reqPath = basePath+'merchantManager/queryMerchantById.action'+url;
			var sendBody = '{}';
			send(reqPath, "POST", "application/json", "json", sendBody, function(res)
			{
				if (res.result == 'success')
				{
					if (res.data != null)
					{
						var merchant = res.data;
						$("#uu_id").val(merchant.uuid);
						$("#business_name").val(merchant.businessName);
						$("#business_scope").val(merchant.businessScope);
						$("#address").val(merchant.address);
						$("#description").val(merchant.description);
						queryUser(merchant.userKey);
						queryMerchantType(merchant.merchantTypeKey);
						queryCity(merchant.cityKey);
					} else
					{
						alert("没有查找到相关数据");
					}
				}
			});
		}else if (url.indexOf("?type=") != -1)
		{
			queryUser(null);
			queryMerchantType(null);
			queryCity(null);
		}else
		{
			//进页面默认加载列表
			iniadinfoData(0,15);
		}
		$('#addnew').click(function()
		{
			window.location.href = jumpPath + 'view/technology/merchant/addmerchant.jsp?type=addmerchant';
		});

		// 查询
		$('#btn_search').bind('click', function()
		{
			iniadinfoData(0,15);
		});

		$('#tbn_save').bind('click', function()
		{
			savemerchant();
//			window.location = jumpPath + 'view/technology/merchanttype/merchanttypelist.jsp';
		});
		
		$('#tbn_update').bind('click', function()
		{
			updatemerchanttype();
		});
		
	});// the end

	/* @-----------------------------------------------------------------------------------------@ */
})(jQuery, window);

function queryUser(userKey)
{
	var requsetPath = basePath + "userManager/queryUserList.action";
	send(requsetPath, "POST", "application/json", "json", "{}",
			function(resultData)
			{

				if (resultData.result == 'success')
				{
					$.each(resultData.data, function(i,n)
					{
						$("#user_key").append("<option value='"+n.uuid+"'>"+n.userName+"</option>");
					});
					if(null!=userKey)
					{
						$("#user_key").val(userKey);
					}
				} else
				{
					alert(resultData.errorMsg);
				}
			});
}

function queryMerchantType(merchantTypeKey)
{
	var requsetPath = basePath + "merchantTypeManager/queryMerchantTypeList.action";
	send(requsetPath, "POST", "application/json", "json", '{"typeStrategy":2}',
			function(resultData)
			{

				if (resultData.result == 'success')
				{
					$.each(resultData.data, function(i,n)
					{
						$("#merchantType_key").append("<option value='"+n.uuid+"'>"+n.typeName+"</option>");
					});
					if(null!=merchantTypeKey)
					{
						$("#merchantType_key").val(merchantTypeKey);
					}
				} else
				{
					alert(resultData.errorMsg);
				}
			});
}

function queryCity(cityKey)
{
	var requsetPath = basePath + "cityManager/queryAllCity.action";
	send(requsetPath, "POST", "application/json", "json", "{}",
			function(resultData)
			{

				if (resultData.result == 'success')
				{
					$.each(resultData.data, function(i,n)
					{
						$("#city_key").append("<option value='"+n.uuid+"'>"+n.cityName+"</option>");
					});
					if(null!=cityKey)
					{
						$("#city_key").val(cityKey);
					}
				} else
				{
					alert(resultData.errorMsg);
				}
			});
}

function editmerchant(merchantId)
{
	window.location.href = jumpPath + 'view/technology/merchant/editmerchant.jsp?merchantId='+merchantId;
}

function del(id)
{

	if (confirm("确定要删除吗？"))
	{
		var reqPath = basePath+'merchantManager/deleteMerchant.action?merchantId='+id;
		send(reqPath, "POST", "application/json", "json", null, function(res)
		{
			if (res.result == 'success')
			{
				iniadinfoData(0,15);
			}
		});
	}
}

function iniadinfoData(pageNumber,pagesize)
{

	var reqPath = basePath+'merchantManager/queryMerchantByPage.action';
	var sendBody = '{"currentPage": 1,"pageSize": 15}';
	var $tab = $('#adinfoDataTables tbody').html("");

	
	var reqPath = basePath + 'merchantManager/queryMerchantByPage.action';
	var sendBody = '{"currentPage": '+pageNumber+',"pageSize": '+pagesize+'}';
	var $tab = $('#merchanttypeDataTables tbody').html("");

	send(reqPath, "POST", "application/json", "json", sendBody, function(res)
	{

		if (res.result == 'success')
		{

			if (res.data.data.length > 0)
			{
				var tpl_tr = $('#tpl_list').html(), rows = '';
				$.each(res.data.data, function(i,n)
				{
					var auditStatus = '正在审核';
					var operation = "<button onclick='examine(\""+n.uuid+"\")' type='button' class='btn btn-primary'>审核</button>"+
									" <button onclick='editmerchant(\""+n.uuid+"\")' type='button' class='btn btn-primary'>编辑</button>";
					if(null!=n.auditStatus&&n.auditStatus==1)
					{
						auditStatus = '审核通过';
						operation = " <button onclick='editmerchant(\""+n.uuid+"\")' type='button' class='btn btn-primary'>编辑</button>";
					}
					operation += " <button onclick='del(\""+n.uuid+"\")' type='button' class='btn btn-primary'>删除</button>";
					if(n.top==0)
					{
						operation += " <button onclick='topmerchant(\""+n.uuid+"\")' type='button' class='btn btn-primary'>置顶</button>";
					}else
					{
						console.log("zengqijun"+n.top);
						operation += " <button onclick='canceltop(\""+n.uuid+"\",\""+n.top+"\")' type='button' class='btn btn-primary'>撤销</button>";
					}
					rows += tpl_tr.replace(/\$businessName/g, n.businessName)
								  .replace(/\$businessScope/,n.businessScope)
								  .replace(/\$address/, n.address)
								  .replace(/\$description/, n.description)
								  .replace(/\$userKey/, n.userName)
								  .replace(/\$merchantTypeKey/, n.typeName)
								  .replace(/\$cityKey/, n.cityName)
								  .replace(/\$createAt/, getLocalTime(n.createAt.toString().substr(0,10)))
								  .replace(/\$auditStatus/, auditStatus)
								  .replace(/\$operation/,operation);
				});
				$tab.append(rows);
				// page
				pagefunction(iniadinfoData,res.data.totalPages,pagesize);

			} else
			{
				$tab.append('<tr><td colspan="7">没有查找到相关数据。</td></tr>');
			}
		}
	});

}

function getLocalTime(nS) { 
	return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " "); 
} 

/**
 * 
 * @METHODNAME: saveLoginadinfo
 * @METDHOD:
 * @Description: 添加登陆用户
 * @author Administrator
 * @date 2015年1月10日 下午5:37:52
 */
function savemerchant()
{
 	var requsetPath = basePath + "merchantManager/insertMerchant.action";
	var requestData = $('#frm_merchant').serializeObject();
	requestData = JSON.stringify(requestData);
	// alert(requestData);
	send(requsetPath, "POST", "application/json", "json", requestData,
			function(resultData)
			{

				if (resultData.result == 'success')
				{
					// 跳转到列表
					window.location = jumpPath
							+ 'view/technology/merchant/merchantlist.jsp';
				} else
				{
					alert(resultData.errorMsg);
				}
			});
}


function updatemerchanttype()
{
	var requsetPath = basePath + "merchantManager/updateMerchant.action";
	var requestData = $('#frm_merchant').serializeObject();
	requestData = JSON.stringify(requestData);
	console.log(requestData);
	send(requsetPath, "POST", "application/json", "json", requestData,
			function(resultData)
			{

				if (resultData.result == 'success')
				{
					// 跳转到列表
					window.location = jumpPath
							+ 'view/technology/merchant/merchantlist.jsp';
				} else
				{
					alert(resultData.errorMsg);
				}
			});
}

function examine(merchantid)
{
	var requsetPath = basePath + "merchantManager/updateMerchant.action";
	var requestData = '{"uuid":"'+merchantid+'","auditStatus":1}';
	send(requsetPath, "POST", "application/json", "json", requestData,
			function(resultData)
			{
				if (resultData.result == 'success')
				{
					// 跳转到列表
					window.location = jumpPath
							+ 'view/technology/merchant/merchantlist.jsp';
				} else
				{
					alert(resultData.errorMsg);
				}
			});
}

function topmerchant(merchantid)
{
	var requsetPath = basePath + "merchantManager/updateMerchant.action";
	var requestData = '{"uuid":"'+merchantid+'","top":0}';
	send(requsetPath, "POST", "application/json", "json", requestData,
			function(resultData)
			{
				if (resultData.result == 'success')
				{
					// 跳转到列表
					window.location = jumpPath
							+ 'view/technology/merchant/merchantlist.jsp';
				} else
				{
					alert(resultData.errorMsg);
				}
			});
}

function canceltop(merchantid,top)
{
	var requsetPath = basePath + "merchantManager/updateMerchant.action";
	var requestData = '{"uuid":"'+merchantid+'","top":'+top+'}';
	send(requsetPath, "POST", "application/json", "json", requestData,
			function(resultData)
			{
				if (resultData.result == 'success')
				{
					// 跳转到列表
					window.location = jumpPath
							+ 'view/technology/merchant/merchantlist.jsp';
				} else
				{
					alert(resultData.errorMsg);
				}
			});
}

function queryParentType()
{
	var requsetPath = basePath + "merchantTypeManager/queryParentMerchantType.action";
	send(requsetPath, "POST", "application/json", "json", "{}",
			function(resultData)
			{

				if (resultData.result == 'success')
				{
					$.each(resultData.data, function(i,n)
					{
						$("#parent_id").append("<option value='"+n.uuid+"'>"+n.typeName+"</option>");
					});
				} else
				{
					alert(resultData.errorMsg);
				}
			});
}

function queryAdList()
{
	debugger;
	var requsetPath = basePath + "adManager/queryAdList.action";
	send(requsetPath, "POST", "application/json", "json", "{}",
			function(resultData)
			{

				if (resultData.result == 'success')
				{
					var adInfo = '';
					$.each(resultData.data, function(i,n)
					{
						adInfo += '<input type="checkbox" name="bindToId" value="'+n.uuid+'"/>'+n.businessName+' '
					});
					$("#ad_info").append(adInfo);
				} else
				{
					alert(resultData.errorMsg);
				}
			});
}
